classDiagram
    %% 基础MFC类
    class CWinApp {
        <<MFC Framework>>
        +InitInstance() BOOL
        +ExitInstance() int
    }
    
    class CDialogEx {
        <<MFC Framework>>
        +OnInitDialog() BOOL
        +DoDataExchange(pDX) void
        +OnPaint() void
    }
    
    class CAsyncSocket {
        <<MFC Framework>>
        +Create(nSocketPort) BOOL
        +Connect(lpszHostAddress, nHostPort) BOOL
        +Listen(nConnectionBacklog) BOOL
        +Accept(rConnectedSocket) BOOL
        +Send(lpBuf, nBufLen) int
        +Receive(lpBuf, nBufLen) int
        +Close() void
        +AsyncSelect(lEvent) BOOL
        +OnConnect(nErrorCode) void
        +OnAccept(nErrorCode) void
        +OnReceive(nErrorCode) void
        +OnClose(nErrorCode) void
    }

    %% 服务端应用程序类
    class CChatRoomServerApp {
        +InitInstance() BOOL
        +ExitInstance() int
    }

    %% 客户端应用程序类
    class CChatRoomClientApp {
        +InitInstance() BOOL
        +ExitInstance() int
    }

    %% 服务端Socket类
    class CServerSocket {
        -m_pParent CChatRoomServerDlg*
        +CServerSocket()
        +~CServerSocket()
        +OnAccept(nErrorCode) void
        +SetParent(pParent) void
    }

    class CClientSocketEx {
        -m_pParent CChatRoomServerDlg*
        -m_userID int
        +CClientSocketEx()
        +~CClientSocketEx()
        +OnReceive(nErrorCode) void
        +OnClose(nErrorCode) void
        +SetParent(pParent) void
        +SetUserID(userID) void
        +GetUserID() int
    }

    %% 客户端Socket类
    class CClientSocket {
        -m_pParent CChatRoomClientDlg*
        +CClientSocket()
        +~CClientSocket()
        +OnConnect(nErrorCode) void
        +OnReceive(nErrorCode) void
        +OnClose(nErrorCode) void
        +SetParent(pParent) void
    }

    %% 服务端主对话框类
    class CChatRoomServerDlg {
        -m_serverSocket CServerSocket
        -m_clientSockets map
        -m_users map
        -m_nextUserID int
        -m_bServerRunning bool
        -m_editIP CEdit
        -m_editPort CEdit
        -m_editLog CEdit
        -m_listUsers CListCtrl
        +CChatRoomServerDlg(pParent)
        +~CChatRoomServerDlg()
        +OnInitDialog() BOOL
        +OnBnClickedStartServer() void
        +OnBnClickedStopServer() void
        +OnSocketAccept(wParam, lParam) LRESULT
        +OnSocketReceive(wParam, lParam) LRESULT
        +OnSocketClose(wParam, lParam) LRESULT
        +ProcessMessage(userID, msg) void
        +BroadcastMessage(msg, excludeUserID) void
        +SendToUser(userID, msg) void
        +AddLogMessage(message) void
        +UpdateUserList() void
        +RemoveUser(userID) void
    }

    %% 客户端主对话框类
    class CChatRoomClientDlg {
        -m_clientSocket CClientSocket
        -m_chatHistory vector
        -m_nickname CString
        -m_bConnected bool
        -m_editServerIP CEdit
        -m_editServerPort CEdit
        -m_editNickname CEdit
        -m_editChatDisplay CEdit
        -m_editMessage CEdit
        +CChatRoomClientDlg(pParent)
        +~CChatRoomClientDlg()
        +OnInitDialog() BOOL
        +OnBnClickedConnect() void
        +OnBnClickedDisconnect() void
        +OnBnClickedSendPublic() void
        +OnBnClickedSendPrivate() void
        +OnSocketConnect(wParam, lParam) LRESULT
        +OnSocketReceive(wParam, lParam) LRESULT
        +OnSocketClose(wParam, lParam) LRESULT
        +UpdateConnectionStatus(connected) void
        +AddChatRecord(sender, content, timestamp) void
        +DisplayChatHistory() void
        +SendChatMessage(type, content, receiver) void
        +SaveChatToFile() void
    }

    %% 数据结构
    class UserInfo {
        +nickname CString
        +ip CString
        +port UINT
        +socket CClientSocketEx*
    }

    class ChatRecord {
        +sender CString
        +content CString
        +timestamp CString
        +isPrivate bool
        +receiver CString
    }

    class ChatMessage {
        +type int
        +sender char[256]
        +receiver char[256]
        +content char[1024]
        +timestamp char[64]
    }

    %% 枚举类型
    class MessageType {
        <<enumeration>>
        MSG_LOGIN
        MSG_PUBLIC
        MSG_PRIVATE
        MSG_LOGOUT
    }

    %% 继承关系
    CWinApp <|-- CChatRoomServerApp
    CWinApp <|-- CChatRoomClientApp
    CDialogEx <|-- CChatRoomServerDlg
    CDialogEx <|-- CChatRoomClientDlg
    CAsyncSocket <|-- CServerSocket
    CAsyncSocket <|-- CClientSocketEx
    CAsyncSocket <|-- CClientSocket

    %% 组合关系
    CChatRoomServerDlg *-- CServerSocket
    CChatRoomServerDlg *-- CClientSocketEx
    CChatRoomServerDlg *-- UserInfo
    CChatRoomClientDlg *-- CClientSocket
    CChatRoomClientDlg *-- ChatRecord

    %% 关联关系
    CServerSocket --> CChatRoomServerDlg
    CClientSocketEx --> CChatRoomServerDlg
    CClientSocket --> CChatRoomClientDlg
    UserInfo --> CClientSocketEx
    
    %% 依赖关系
    CChatRoomServerDlg ..> ChatMessage
    CChatRoomClientDlg ..> ChatMessage
    CChatRoomServerDlg ..> MessageType
    CChatRoomClientDlg ..> MessageType

    %% 聚合关系
    CChatRoomServerApp o-- CChatRoomServerDlg
    CChatRoomClientApp o-- CChatRoomClientDlg